<?php
class CorePage {
	/**
	 * Items to be displayed on page
	 * @var array
	 */
	protected $items = array();
	/**
	 * Simple xml syntax higlight
	 * @copyright from php.net
	 * @param string $s
	 * @return string
	 */
	function xml_highlight($s) {
		$s = htmlspecialchars($s);
		$s = preg_replace("#&lt;([/]*?)(.*)([\s]*?)&gt;#sU",
			"<font color=\"#0000FF\">&lt;\\1\\2\\3&gt;</font>",$s);
		$s = preg_replace("#&lt;([\?])(.*)([\?])&gt;#sU",
			"<font color=\"#800000\">&lt;\\1\\2\\3&gt;</font>",$s);
		$s = preg_replace("#&lt;([^\s\?/=])(.*)([\[\s/]|&gt;)#iU",
			"&lt;<font color=\"#808000\">\\1\\2</font>\\3",$s);
		$s = preg_replace("#&lt;([/])([^\s]*?)([\s\]]*?)&gt;#iU",
			"&lt;\\1<font color=\"#808000\">\\2</font>\\3&gt;",$s);
		$s = preg_replace("#([^\s]*?)\=(&quot;|')(.*)(&quot;|')#isU",
			"<font color=\"#800080\">\\1</font>=<font color=\"#FF00FF\">\\2\\3\\4</font>",$s);
		$s = preg_replace("#&lt;(.*)(\[)(.*)(\])&gt;#isU",
			"&lt;\\1<font color=\"#800080\">\\2\\3\\4</font>&gt;",$s);
		return nl2br($s);
	}
	function getItem($key) {
		$item = $this->items[$key];
		if (isset($item['examples'])) {
			foreach($item['examples'] as &$example) {
				$tal = new MicroTal();
				if(isset($example['data'])) {
					foreach($example['data'] as $k=>$v) {
						$tal->$k = $v;
					}
				}
				$res = $tal->executeString($example['template']);
				if (in_array('rawresult', $example)) {
					$example['result'] = $res;
				} else {
					$example['result'] = $this->xml_highlight($res);
				}
				$example['template'] = $this->xml_highlight($example['template']);
			}
			unset($example);
		}
		$item['id'] = $key;
		return $item;
	}
	function getItems() {
		$items = array();
		foreach($this->items as $key=>$v) {
			$items[] = $this->getItem($key);
		}
		return $items;
	}
	function getCode($key) {
		return highlight_string($this->examples[$key]['php'], true);
	}
	function getTemplate($key) {
		return $this->xml_highlight($this->examples[$key]['template']);
	}
	function getResult($key) {
		$tal = new MicroTal();
		$res = $tal->executeString($this->examples[$key]['template']);
		return $this->xml_highlight($res);
	}
}

